//
//  YSMQMaterialViewController.m
//  YaSha-iOS
//
//  Created by YaSha_Tom on 2018/6/13.
//  Copyright © 2018年 亚厦装饰股份有限公司. All rights reserved.
//

#import "YSMQMaterialViewController.h"
#import "YSFlowAttachmentViewController.h"
#import "YSBussinessMaterialModel.h"
@interface YSMQMaterialViewController ()
@property (nonatomic, strong) NSMutableArray *expensePersonArr;
@end

@implementation YSMQMaterialViewController

#pragma mark -- 懒加载
- (void)viewDidLoad {
    [super viewDidLoad];
    [self doNetworking];
}

- (void)doNetworking {
    NSString *urlString = [NSString stringWithFormat:@"%@%@/%@",YSDomain, getMaterialDemandFlowDetail, self.cellModel.businessKey];
    [QMUITips showLoadingInView:self.view];
    [YSNetManager ys_request_GETWithUrlString:urlString isNeedCache:NO parameters:nil successBlock:^(id response) {
        [QMUITips hideAllTipsInView:self.view];
        if ([response[@"code"] intValue] == 1) {
            self.flowFormModel = [YSFlowFormModel yy_modelWithJSON:response[@"data"]];
            self.coverNavView.titleLabel.text = self.flowFormModel.baseInfo.title;
            [self.functionHeaderView setHeaderModel:self.flowFormModel.baseInfo];
            [self setUpData:self.flowFormModel.info];
        //附件
            for (NSDictionary *dic in self.flowFormModel.info[@"mobileFiles"]) {
                YSNewsAttachmentModel *model = [YSNewsAttachmentModel yy_modelWithJSON:dic];
                [self.attachArray addObject:model];
            }
            if (self.attachArray.count > 0) {
                [self.functionHeaderView.documentButton setTitle:[NSString stringWithFormat:@"关联文档(%lu)",(unsigned long)self.attachArray.count] forState:UIControlStateNormal];
            }
            [self.tableView reloadData];
            //定位当前的标题位置（该计算要在tableView刷新之后计算来保证header位置的准确）
            [self markSectionHeaderLocation];
        }
    } failureBlock:^(NSError *error) {
        
    } progress:nil];
    
    //提交者附言
    NSString *urlStringPS = [NSString stringWithFormat:@"%@%@/%@", YSDomain, getPostscriptListApi, self.cellModel.businessKey];
    
    [YSNetManager ys_request_GETWithUrlString:urlStringPS isNeedCache:NO parameters:nil successBlock:^(id response) {
        if ([response[@"code"] intValue] == 1){
            for (NSDictionary *postscriptDic in response[@"data"]) {
                [self.postscriptArray addObject:@{@"content":postscriptDic[@"massage"],@"special":@(BussinessFlowCellText)}];
                [self.fileArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[YSNewsAttachmentModel class] json:postscriptDic[@"mobileFileVos"]]];
            }
            
            [self.attachArray addObjectsFromArray:self.fileArray]; 
            
            if (self.attachArray.count > 0 || self.fileArray.count > 0 ||self.documentAr.count > 0) {
                [self.functionHeaderView.documentButton setTitle:[NSString stringWithFormat:@"关联文档(%lu)",self.attachArray.count+self.fileArray.count+self.documentAr.count] forState:UIControlStateNormal];
            }
        }
    } failureBlock:^(NSError *error) {
        
    } progress:nil];
    
    //审批记录
    NSString *urlStringRecord = [NSString stringWithFormat:@"%@%@/%@/1", YSDomain, getCommentsByProcessInstanceIdApi, self.cellModel.processInstanceId];
    DLog(@"=========%@",urlString);
    [YSNetManager ys_request_GETWithUrlString:urlStringRecord isNeedCache:NO parameters:nil successBlock:^(id response) {
        DLog(@"审批记录:%@", response);
        self.recordArray = [[YSDataManager getFlowRecordListData:response] mutableCopy];
        [self.tableView reloadData];
        //定位当前的标题位置（该计算要在tableView刷新之后计算来保证header位置的准确）
        dispatch_async(dispatch_get_main_queue(), ^{
            [self markSectionHeaderLocation];
        });
        
    } failureBlock:^(NSError *error) {
        
    } progress:nil];
    
    //转阅记录列表
    NSString *urlStringTurn = [NSString stringWithFormat:@"%@%@/%@/2", YSDomain, getCommentsByProcessInstanceIdApi, self.cellModel.processInstanceId];
    DLog(@"=========%@",urlString);
    [YSNetManager ys_request_GETWithUrlString:urlStringTurn isNeedCache:NO parameters:nil successBlock:^(id response) {
        DLog(@"转阅记录列表:%@", response);
        self.turnArray = [[YSDataManager getFlowRecordListData:response] mutableCopy];
        [self.tableView reloadData];
        //定位当前的标题位置（该计算要在tableView刷新之后计算来保证header位置的准确）
        dispatch_async(dispatch_get_main_queue(), ^{
            [self markSectionHeaderLocation];
        });
    } failureBlock:^(NSError *error) {
        
    } progress:nil];
}
- (void)setUpData:(NSDictionary *)dic {
    self.expensePersonArr = [NSMutableArray array];
    YSBussinessMaterialModel *materModel = [YSBussinessMaterialModel yy_modelWithJSON:dic];
    [self.expensePersonArr addObject:@{@"title":@"基本信息",@"special":@(BussinessFlowCellHead)}];
    [self.expensePersonArr addObject:@{@"title":@"项目名称",@"content":materModel.proName}];
    [self.expensePersonArr addObject:@{@"title":@"项目编码" ,@"content":materModel.proCode}];
    [self.expensePersonArr addObject:@{@"title":@"材料需求编号" ,@"content":materModel.code}];
    [self.expensePersonArr addObject:@{@"title":@"项目经理" ,@"content":materModel.code}];
    [self.expensePersonArr addObject:@{@"title":@"材料类型" ,@"content":materModel.mtrlTypeStr}];
    [self.expensePersonArr addObject:@{@"title":@"三级类别" ,@"content":materModel.threeTypeName}];
    [self.expensePersonArr addObject:@{@"title":@"是否甲供材" ,@"content":materModel.isSupply ? @"是":@"否"}];
    [self.expensePersonArr addObject:@{@"title":@"需求类别" ,@"content":materModel.requireTypeStr}];
    if (![materModel.requireTypeStr isEqualToString:@"正常下单"]) {
        [self.expensePersonArr addObject:@{@"title":@"补料原因" ,@"content":materModel.feedReason}];
    }
    
    [self.expensePersonArr addObject:@{@"title":@"收货人" ,@"content":materModel.receiverName }];
    [self.expensePersonArr addObject:@{@"title":@"收货人联系电话" ,@"content":materModel.receiverMobile}];
    [self.expensePersonArr addObject:@{@"title":@"收货地址",@"content":[NSString stringWithFormat:@"%@%@%@%@",materModel.province,materModel.city,materModel.area,materModel.address]}];
    
    [self.expensePersonArr addObject:@{@"title":@"备注" ,@"content":materModel.remark}];
    self.dataSourceArray = self.expensePersonArr;
    
}
//在数据加载完后，从新设置定位位置
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self markSectionHeaderLocation];
}
# pragma mark -- tableView deletage
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
        {
            YSFlowFormListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YSFlowFormListCell"]; //出列可重用的cell
            
            if (cell == nil) {
                cell = [[YSFlowFormListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YSFlowFormListCell"];
            }
            [cell setCommonBusinessFlowDetailWithDictionary:self.dataSourceArray[indexPath.row]];
            return cell;
        }
            break;
        case 1:
        {
            YSFlowFormListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YSFlowFormListCell"]; //出列可重用的cell
            if (cell == nil) {
                cell = [[YSFlowFormListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YSFlowFormListCell"];
            }
            [cell setCommonBusinessFlowDetailWithDictionary:self.postscriptArray[indexPath.row]];
            return cell;
        }
            break;
        case 2:
        {
            YSFlowRecordListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellExaminationList]; //出列可重用的cell
            if (cell == nil) {
                cell = [[YSFlowRecordListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellExaminationList];
            }
            if (self.recordArray.count > 0) {
                YSFlowRecordListModel *cellModel = self.recordArray[indexPath.row];
                [cell setRecordListCellModel:cellModel andIndexPath:indexPath];
                [self callPhone:cellModel.userId andTableViewCell:cell];
            }
            return cell;
        }
            break;
        default:
        {
            YSFlowRecordListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierFlow]; //出列可重用的cell
            if (cell == nil) {
                cell = [[YSFlowRecordListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierFlow];
            }
            if (self.turnArray.count > 0) {
                YSFlowRecordListModel *cellModel = self.turnArray[indexPath.row];
                [cell setRecordListCellModel:cellModel andIndexPath:indexPath];
                [self callPhone:cellModel.userId andTableViewCell:cell];
            }
            return cell;
        }
            break;
    }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
